package com.jason.sms.agent.util;

import java.io.IOException;
import java.util.Properties;

import org.slf4j.LoggerFactory;

/**
 * 判断号码类型工具类
 * 用位置检索法,纳秒级消耗
 */
public class PhoneNumberUtil {
    //4段快速定位
    private static int[][][][] phoneMap = new int[10][10][10][10];

    private static final String PHONE_TYPE_CONFIG_PATH = "/phoneNumberType.properties";

    static {
        //        initMap();
        Properties pro = new Properties();
        try {
            pro.load(PhoneNumberUtil.class.getResourceAsStream(PHONE_TYPE_CONFIG_PATH));
        } catch (IOException e) {
//            e.printStackTrace();
            LoggerFactory.getLogger("ESGAGENT").error("严重错误:加载号码区段配置文件异常!!!", e);
        }
        for (Object o_key : pro.keySet()) {
            String key = (String) o_key;
            int value = Enum.valueOf(PhoneNumberType.class, pro.getProperty(key)).getValue();
            String[] keys = key.split(",");
            for (String oneNum : keys) {
                int[] num = new int[oneNum.length()];
                for (int u = 0; u < oneNum.length(); u++) {
                    num[u] = oneNum.charAt(u) - 48;
                }
                fill(value, num);
            }
        }
    }

    /**
     * 初始化填充
     * 只支持到前四位检索
     *
     * @param key
     * @param num
     */
    private static void fill(int key, int... num) {
        switch (num.length) {
            case 1:
                for (int i = 0; i < 10; i++) {
                    for (int j = 0; j < 10; j++) {
                        for (int k = 0; k < 10; k++) {
                            phoneMap[num[0]][i][j][k] = key;
                        }
                    }
                }
                break;
            case 2:
                for (int i = 0; i < 10; i++) {
                    for (int j = 0; j < 10; j++) {
                        phoneMap[num[0]][num[1]][i][j] = key;
                    }
                }
                break;
            case 3:
                for (int i = 0; i < 10; i++) {
                    phoneMap[num[0]][num[1]][num[2]][i] = key;
                }
                break;
            default:
                phoneMap[num[0]][num[1]][num[2]][num[3]] = key;
        }
    }

    /**
     * 通过前四位判断
     *
     * @param phone
     * @return
     */
    public static PhoneNumberType getPhoneType(String phone) {
        int[] num = new int[4];
        for (int u = 0; u < 4; u++) {
            num[u] = phone.charAt(u) - 48;
        }
        return PhoneNumberType.getType(phoneMap[num[0]][num[1]][num[2]][num[3]]);
    }
    
    public static void main(String[] arg){
    	System.out.println(getPhoneType("13316098312").name());
    }

}
